#pragma once

#include "vtkOpenCVBridge.h"

void vtkOpenCVBridge::vector_cvMatTogpuMat ()
{
	for (std::vector<cv::Mat>::iterator it = m_vectorCVMat.begin(); it!=m_vectorCVMat.end(); ++it)
	{
		cv::Mat temp_Mat = *it;
		cv::gpu::GpuMat temp_GpuMat (temp_Mat);
		m_vectorGPUMat.push_back (temp_GpuMat);
	}
}

void vtkOpenCVBridge::vector_gpuMatToCvMat ()
{
	for (std::vector<cv::gpu::GpuMat>::iterator it = m_vectorGPUMat.begin(); it!=m_vectorGPUMat.end(); ++it)
	{
		cv::gpu::GpuMat temp_GpuMat = *it;
		cv::Mat temp_Mat (temp_GpuMat);
		m_vectorCVMat.push_back (temp_Mat);
	}
}

vtkSmartPointer<vtkImageData> vtkOpenCVBridge::OpenCVtoVTK ()
{
	m_imageData->SetExtent(0, m_vectorCVMat[0].cols, 0, m_vectorCVMat[0].rows, 0, (int)m_vectorCVMat.size());
	m_imageData->SetScalarTypeToUnsignedChar();

	for (int vol_dim=0; vol_dim<m_vectorCVMat.size(); ++vol_dim)
	{
		for (int i=0; i<m_vectorCVMat[0].cols; ++i)
		{
			unsigned char* mat_ptr = m_vectorCVMat[vol_dim].ptr<unsigned char>(i);
			for (int j=0; j<m_vectorCVMat[0].rows; ++j)
				*static_cast<unsigned char*>(m_imageData->GetScalarPointer(j,i,vol_dim)) = mat_ptr[j];
		}
	}
	return m_imageData;
}

std::vector<cv::gpu::GpuMat> vtkOpenCVBridge::VTKtoOpenCV_gpu ()
{
	m_imageData->SetScalarTypeToUnsignedChar();

	for (int vol_dim=0; vol_dim<m_dimensions[2]; ++vol_dim)
	{
		cv::Mat temp_Mat(m_dimensions[1], m_dimensions[0], CV_8UC1);
		for (int i=0; i<m_dimensions[1]; ++i)
		{
			unsigned char* mat_ptr = temp_Mat.ptr<unsigned char>(i);
			for (int j=0; j<m_dimensions[0]; ++j)
				mat_ptr[j] = *static_cast<unsigned char*>(m_imageData->GetScalarPointer(j,i,vol_dim));
		}
		m_vectorCVMat.push_back(temp_Mat);
	}

	vtkOpenCVBridge::vector_cvMatTogpuMat();
	return m_vectorGPUMat;
}

std::vector<cv::Mat> VTKtoOpenCV_mat ()
{
	m_imageData->SetScalarTypeToUnsignedChar();
}   
    for (int vol_dim=0; vol_dim<m_dimensions[2]; ++vol_dim)
    {
    	cv::Mat temp_Mat(m_dimensions[1], m_dimensions[0], CV_8UC1);
    	for (int i=0; i<m_dimensions[1]; ++i)
    	{
    		unsigned char* mat_ptr = temp_Mat.ptr<unsigned char>(i);
    		for (int j=0; j<m_dimensions[0]; ++j)
    			mat_ptr[j] = *static_cast<unsigned char*>(m_imageData->GetScalarPointer(j,i,vol_dim));
    	}
    	m_vectorCVMat.push_back(temp_Mat);
    }
    
    return m_vectorCVMat;